package se.webbzon.boltzmann.minigame.question;

import java.awt.Color;
import java.awt.Font;

import se.webbzon.boltzmann.minigame.page.Page;
import se.webbzon.boltzmann.minigame.page.PageItem;
import se.webbzon.boltzmann.minigame.page.PageLayout;
import se.webbzon.boltzmann.minigame.page.PageText;
import se.webbzon.oschi01.font.HorizontalAlignment;
import se.webbzon.oschi01.font.VerticalAlignment;
import se.webbzon.oschi01.worldcamera.WorldCameraPane;

public class AlternativeItem extends PageText implements PageItem {
	
	// The maximal size of the selection circle
	private static final double maxSize = 12;
	
	// The alternative index
	private final int alt;
	
	// The size of the selection circle
	private double t;
	private volatile double size;
	
	// Whatever this answer is selected
	private boolean selected;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public AlternativeItem(PageLayout layout, int alt, String text) {
		super(layout,text);
		this.alt = alt;
		selected = alt == 1;
		t = 0;
		size = 0;
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Returns the alternative index of this alternative. **/
	public int getAlternative() {
		return alt;
	}
	
	/** Returns true if this alternative is selected. **/
	public boolean isSelected() {
		return selected;
	}

	/** Sets whatever this alternative is selected. **/
	public void setSelected(boolean selected) {
		this.selected = selected;
	}
	
	@Override public void draw(WorldCameraPane hud, int x, int y) {
		final PageLayout layout = getLayout();
		final Font font = layout.getFont(0, true, false);
		hud.setFont(font);
		hud.setHAlignment(HorizontalAlignment.LEFT);
		hud.setVAlignment(VerticalAlignment.TOP);
		if (isSelected()) {
			final int yOffset = font.getSize()/2 - 2;
			final int xOffset = 4 + (int) maxSize / 2;
			final int s = (int) size;
			final int hs = s / 2;
			hud.setColor(Color.blue);
			hud.fillOval(x + xOffset - hs, y + yOffset - hs, s, s);
			hud.drawString("Alt " + alt, x + 24, y);
		} else
			hud.drawString("Alt " + alt, x + 4, y);
		super.draw(hud, x, y + font.getSize() + layout.getLineSpacing());
	}

	@Override public int getHeight() {
		final PageLayout layout = getLayout();
		final Font font = layout.getFont(0, true, false);
		return super.getHeight() + font.getSize() + layout.getLineSpacing();
	}
	
	@Override public void onStep(Page page) {
		if (isSelected()) {
			size = maxSize * Math.abs(Math.sin(t));
			t += Math.PI / 60;
		} else
			t = 0;
	}

}
